#include "editor/floodfillcommand.hpp"

namespace nmc
{
    FloodFillCommand::FloodFillCommand(TileMap* tileMap, 
                                       int x_, 
                                       int y_, 
                                       int width,
                                       int height,
                                       int tile, 
                                       int tileFlags)
         :mTileMap(tileMap),
         mX(x_),
         mY(y_),
         mWidth(width),
         mHeight(height),
         mTile(tile),
         mTileFlags(tileFlags)
     {
         int x;
         int y;
         for (y = mY; y < mY + mHeight; y++)
         {
            for (x = mX; x < mX + mWidth; x++)
            {
                mOldTiles.push_back(mTileMap->getTile(x, y));
                mOldTileFlags.push_back(mTileMap->getTileFlags(x, y));
            }
         }
     }

     void FloodFillCommand::execute()
     {
         int x;
         int y;
         for (y = mY; y < mY + mHeight; y++)
         {
            for (x = mX; x < mX + mWidth; x++)
            {
                mTileMap->setTile(x, y, mTile);
                mTileMap->setTileFlags(x, y, mTileFlags);
            }
         }
     }

     void FloodFillCommand::undo()
     {
         int x;
         int y;
         int i = 0;
         for (y = mY; y < mY + mHeight; y++)
         {
            for (x = mX; x < mX + mWidth; x++)
            {
                mTileMap->setTile(x, y, mOldTiles[i]);
                mTileMap->setTileFlags(x, y, mOldTileFlags[i]);
                i++;
            }
         }
     }
}